<?php
class MembersController extends MembersAppController {
  	var $name = 'Member';
	var $uses = array('Members.Member','Members.MemberUpload');
	var $permissions = array(
		'add' => array('admin','manag'),
		'edit' => array('admin','manag'),
		'delete' => array('admin','manag')
	);


	public function beforeFilter(){
		parent::beforeFilter();
	}
	
	public function index(){
		$members = $this->Member->find('all');

	}
	
	function add(){
		if(!empty($this->data)){
			if($this->FileUpload->success){
				$this->data['Member']['upload_id'] = $this->MemberUpload->getLastInsertID();				 
			}else{
				$this->Session->setFlash($this->FileUpload->showErrors());
			}
			$this->data['Member']['added'] = date('Y-m-d H:i:s');			
			$lp = $this->Member->find('first',array('fields'=>array('MAX(Member.lp) AS lp')));
			$this->data['Member']['lp'] = $lp['0']['lp']+1;
			
			$this->Member->create();
			if ($this->Member->save($this->data)) {
				$this->Session->setFlash(__('The member has been saved', true));
			} else {
				$this->Session->setFlash(__('The member could not be saved. Please, try again.', true));
			}
			$this->redirect($this->referer());
		}
	}
	
	function edit($id=null){
		if(!empty($this->data)){
			if($this->FileUpload->success){
				$this->data['Member']['upload_id'] = $this->MemberUpload->getLastInsertID();
			}
			
			if ($this->Member->save($this->data)) {
				$this->Session->setFlash(__('The member has been saved', true));
			} else {
				$this->Session->setFlash(__('The member could not be saved. Please, try again.', true));
			}	
			$this->redirect($this->referer());			
		}
		$this->data = $this->Member->findById($id);
	}	
	
	function delete($id = null) {
		$Member = $this->Member->findById($id);
	    $upload =  $this->MemberUpload->findById($Member['Member']['upload_id']);	    
	    if($this->FileUpload->removeFile($upload['MemberUpload']['name'])){
			if($this->MemberUpload->delete($upload['MemberUpload']['id'])){
	        	
			}else{
				$this->Session->setFlash(__('The member could not be deleted from MemberUpload. Please, try again.', true));
			}
	    }else{
	    	$this->Session->setFlash(__('The member could not be deleted from Disc. Please, try again.', true));
	    }
	    if($this->Member->delete($Member['Member']['id'])){
        	$this->Session->setFlash('Member deleted');
      	}else{
      		$this->Session->setFlash(__('The member could not be deleted from Member. Please, try again.', true));
      	}
	    $this->redirect($this->referer());
	}
	
	function delete_gallery($category_id = null) {
		$Member = $this->Member->find('list',array(
			'conditions'=>array('Member.category_id'=>$category_id),
			'fields'=>array('Member.id','Member.upload_id')));
		foreach ($Member as $gallery_id => $upload_id) {
			$upload =  $this->MemberUpload->findById($upload_id);	    
		    if($this->FileUpload->removeFile($upload['MemberUpload']['name'])){
				if($this->MemberUpload->delete($upload['MemberUpload']['id'])){
			      	if($this->Member->delete($gallery_id)){
			        	$this->Session->setFlash('File deleted');
			      	}else{
			      		$this->Session->setFlash(__('The picture could not be deleted from Member. Please, try again.', true));
			      	}
		        	
				}else{
					$this->Session->setFlash(__('The picture could not be deleted from MemberUpload. Please, try again.', true));
				}
		    }else{
		    	$this->Session->setFlash(__('The picture could not be deleted from Disc. Please, try again.', true));
		    }
		}	    
	    $this->redirect($this->referer());
	}	
	
	function up($id=null){
		
		$a = $this->Member->read(null, $id);
		$b = $this->Member->find('first',array(
	        'conditions' => array(
				'Member.category_id = ' =>$a['Member']['category_id'],
				'Member.lp = ' => $a['Member']['lp'] - 1				
			)));
				
		if(is_array($a) && !empty($a)){
			$a['Member']['lp'] = (isset($a['Member']['lp']) && $a['Member']['lp'] > 1 ) ? $a['Member']['lp'] - 1 : 1;
			$this->Member->save($a);
			if(is_array($b) && !empty($b)){
				$b['Member']['lp'] = (isset($b['Member']['lp']) ) ? $b['Member']['lp'] + 1 : 0;
				$this->Member->save($b);	
			}			
		}
		$this->redirect($this->referer());
	}	
	
	function down($id=null){
				
		$a = $this->Member->read(null, $id);		
		$b = $this->Member->find('first',array(
	        'conditions' => array(
				'Member.category_id = ' =>$a['Member']['category_id'],
				'Member.lp = ' => $a['Member']['lp'] + 1				
			)));
	
		if(is_array($a) && !empty($a)){
			$a['Member']['lp'] = (isset($a['Member']['lp']) ) ? $a['Member']['lp'] + 1 : 0;		
			$this->Member->save($a);
			if(is_array($b) && !empty($b)){
				$b['Member']['lp'] = (isset($b['Member']['lp']) ) ? $b['Member']['lp'] - 1 : 0;
				$this->Member->save($b);	
			}			
		}
		$this->redirect($this->referer());
	}
}
?>